﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OAModel;
using Telerik.OpenAccess.Data.Common;
using Telerik.OpenAccess.Metadata.Relational;

namespace ITMan
{
    public partial class FormCheckNodeLink : Form
    {
        public FormCheckNodeLink()
        {
            InitializeComponent();
        }

        private void tbContent_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btQuery_Click(sender, e);
        }

        private void btQuery_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbContent.Text) || comboCondition.SelectedIndex == -1)
            {
                MessageBox.Show(@"请先选择类型并输入数据！");
            }
            else
            {
                switch (comboCondition.SelectedIndex)
                {
                    case -1:
                        //MessageBox.Show(@"请先选择类型并输入数据！");
                        break;
                    case 0:
                        {
                            using (OAEntitiesModel dbContext = new OAEntitiesModel())
                            {
                                string sql =
                                    "SELECT  requestid ,requestname ,requestmark ,creater ,createdatetime ,LastoperateNode ,"
                                    +"LastOperator ,LastOperatDatetime ,CurrentNode , CurrentOperator ,AcceptDatetime ," +
                                    "CompleteDatetime FROM dbo.VW_WorkflowInfo WHERE requestid=@requestid";
                                OAParameter parameter=new OAParameter();
                                parameter.ParameterName = "requestid";
                                parameter.DbType = System.Data.DbType.Int32;
                                parameter.Value = tbContent.Text;
                                IEnumerable<VW_WorkflowInfo> workflowInfos = dbContext.ExecuteQuery<VW_WorkflowInfo>(
                                    sql, parameter);
                                if (workflowInfos.Any())
                                    dataGridViewResult.DataSource = workflowInfos.ToList();
                            }
                        }
                        break;
                    case 1:
                        {
                            using (OAEntitiesModel dbContext = new OAEntitiesModel())
                            {
                                IEnumerable<Workflow_requestbase> bases = from i in dbContext.Workflow_requestbases
                                                                          where i.Requestmark == tbContent.Text
                                                                          select i;
                                if (bases.Any())
                                {
                                    IEnumerable<VW_WorkflowInfo> workflowInfos = from i in dbContext.VW_WorkflowInfos
                                                                                 where i.Requestid == bases.FirstOrDefault().Requestid
                                                                                 select i;
                                    if (workflowInfos.Any())
                                        dataGridViewResult.DataSource = workflowInfos.ToList();
                                }

                            }
                        }
                        break;
                    case 2:
                        {
                            using (OAEntitiesModel dbContext = new OAEntitiesModel())
                            {
                                IEnumerable<Workflow_requestbase> bases = from i in dbContext.Workflow_requestbases
                                                                          where i.Requestname == tbContent.Text
                                                                          select i;
                                if (bases.Any())
                                {
                                    IEnumerable<VW_WorkflowInfo> workflowInfos = from i in dbContext.VW_WorkflowInfos
                                                                                 where i.Requestid == bases.FirstOrDefault().Requestid
                                                                                 select i;
                                    if (workflowInfos.Any())
                                        dataGridViewResult.DataSource = workflowInfos.ToList();
                                }

                            }
                        }
                        break;
                }
                tbContent.Text = null;
                comboCondition.SelectedIndex = -1;
            }

            
        }

    }

}
